layui.use(['table', 'laypage', 'layer', 'form', 'laytpl'], function() {
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laytpl = layui.laytpl;
	var ordernumber;
	$("#searchBtn").click(function() {
		ordernumber = $("#ordernumber").val();
		getundistribute()
	});
	$("#reLoad").click(function() {
		ordernumber = "";
		layer.msg("刷新", {
			time: 500
		}, function() {
			getundistribute()
		});
	});
	getundistribute();

	function getundistribute() {
		$.ajax({
			type: "post",
			url: "/distribute/unfinish",
			async: true,
			data: {
				ordernumber: ordernumber
			},
			success: function(data) {
				var result = $.parseJSON(data);
				var tablelist = table.render({
					elem: '#test',
					height: 'full-95',
					page: true, //开启分页,
					limits: [10],
					limit: 10,
					id: 'table_reload',
					cols: [
						[ //表头
							{
								checkbox: true,
								fixed: true
							},
							{
								field: 'orderman',
								title: '客户姓名',
								width: 150,
							}, {
								field: 'ordernumber',
								title: '订单编号',
								width: 150,
								sort: true,
							}, {
								field: 'ordercontxt',
								title: '订单内容',
								width: 150,
							}, {
								field: 'ordertime',
								title: '下单时间',
								width: 130
							}, {
								field: 'orderdate',
								title: '下单日期',
								width: 130
							},
							{
								fixed: 'right',
								title: '操作',
								width: 300,
								align: 'center',
								toolbar: '#barDemo'
							}
						]
					],
					"data": result
				});
			}
		});
	}
	//分页
	//表格里的工具栏监控
	table.on('tool(lay_table_filter)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
		var data = obj.data //获得当前行数据,
		layEvent = obj.event; //获得 lay-event 对应的值
		if(layEvent === 'edit') {
			detail(data.distributeid, "edit");
		} else if(layEvent == 'detail') {
			checkDetails(data.distributeid, "detail");
		} 
	});
	$("#addBtn").on("click", function() {
		detail(" ", "add");
	});
	//表格中的编辑事件
	function detail(id, type) {
		var iframeId = new Date().getTime().toString();
		layer.open({
			id: iframeId,
			title: '配送信息',
			type: 2,
			content: '/static/msg/distribute/distribute_edit.html?id=' + id + "&type=" + type,
			area: ['100%', '100%'],
			anim: -1, //关闭动画 兼容ie6-9需设置此项
			isOutAnim: false, //关闭动画  兼容ie6-9需设置此项
			shadeClose: false,
			btn: ['确认', '返回'],
			yes: function(index, layero) {
				var body = layer.getChildFrame('body', index);
				var orderman = body.find(".orderman").val();
				var ordernumber = body.find(".ordernumber").val();
				var ordercontxt = body.find(".ordercontxt").val();
				var ordertime = body.find(".ordertime").val();
				var orderdate = body.find(".orderdate").val();
				var distributeman = body.find(".distributeman").val();
				var distributestarttime = body.find(".distributestarttime").val();
				var distributeovertime = body.find(".distributeovertime").val();
				var isgo = true;
				if(!orderman || orderman == "" || orderman == undefined) {
					layer.msg('客户名不能为空', {
						time: 2000
					});
					isgo = false;
				}
				if(isgo) {
					if(!ordernumber || ordernumber == "" || ordernumber == undefined) {
						layer.msg('订单编号不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					if(!ordercontxt || ordercontxt == "" || ordercontxt == undefined) {
						layer.msg('订单内容不能空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					if(!ordertime || ordertime == "" || ordertime == undefined) {
						layer.msg('订单时间不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					if(!orderdate || orderdate == "" || orderdate == undefined) {
						layer.msg('订单日期不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					if(!distributeman || distributeman == "" || distributeman == undefined) {
						layer.msg('配送员不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					tijiao();
				}

				function tijiao() {
					$.ajax({
						type: "post",
						url: '/distribute/'+type,
						data: {
							id: id,
							orderman: orderman,
							ordernumber: ordernumber,
							ordercontxt: ordercontxt,
							ordertime: ordertime,
							orderdate: orderdate,
							distributeman:distributeman,
							distributestarttime:distributestarttime,
							distributeovertime:distributeovertime
						},
						async: true,
						success: function(data) {
							layer.msg(data.massage, {
								time: 1000
							}, function() {
								if(data.code == '0') {
									layer.close(index);
									getundistribute();
								}
							});
						}
					});
				}

			}
		});
	}

	function checkDetails(id, type) {
		var iframeId = new Date().getTime().toString();
		layer.open({
			id: iframeId,
			title: '配送信息',
			type: 2,
			content: '/static/msg/distribute/distribute_edit.html?id=' + id + "&type=" + type,
			area: ['100%', '100%'],
			anim: -1, //关闭动画 兼容ie6-9需设置此项
			isOutAnim: false, //关闭动画  兼容ie6-9需设置此项
			shadeClose: false,
			btn: ['确认', '取消'],
			yes: function(index, layero) {
				layer.close(index);
			}
		});
	}

});